from rest_framework import serializers

from goods.models import SKU


class CartSerializer(serializers.Serializer):
    """购物⻋序列化器:校验数据使用"""

    sku_id = serializers.IntegerField(label="商品id", min_value=1)
    count = serializers.IntegerField(label="购买数量", min_value=1)
    selected = serializers.BooleanField(label="商品勾选状态", default=True)

    def validate_sku_id(self, value):

        try:
            SKU.objects.get(id=value)
        except SKU.DoesNotExist:
            raise serializers.ValidationError('SKU 不存在')
        return value


class CartSKUSerializer(serializers.ModelSerializer):
    """购物车查询"""

    count = serializers.IntegerField(label="购买数量")
    selected = serializers.BooleanField(label='商品勾选状态')

    class Meta:
        model = SKU
        fields = ['id', 'name', 'default_image_url', 'price', 'count', 'selected']


class CartDeleteSerializer(serializers.Serializer):
    """    删除购物⻋车数据序列列化器器    """
    sku_id = serializers.IntegerField(label='商品id', min_value=1)

    def validate_sku_id(self, value):
        try:
            sku = SKU.objects.get(id=value)
        except SKU.DoesNotExist:
            raise serializers.ValidationError('商品不不存在')
        return value


class CartSelectAllSerializer(serializers.Serializer):
    """购物⻋全选"""
    selected = serializers.BooleanField(label='全选')

